Ext.define('AmpleHarvest.views.search.saveQuery',
{
	extend			: 'Ext.window.Window',
	alias			: 'widget.ampleharvest-views-search-savequery',
	minimizable		: false,
	maximizable		: true,
	width			: 500,
	shim			: false,
	animCollapse	: false,
	constrainHeader	: true,
	modal			: true,
	requires		:
	[
			"Ext.form.field.Hidden", 'Ext.form.field.Text', 'Ext.form.Panel'
	],

	initComponent	: function()
	{
		var obj_This = this;

		this.callParent();

		this.m_obj_Form = Ext.create("Ext.form.Panel",
		{
			bodyStyle	: 'padding:5px 5px 0',
			frame		: true,
			buttons		:
			[
					{
						text		: 'Cancel',
						disabled	: false,
						scope		: obj_This,
						handler		: function()
						{
							this.close();
						}
					},
					{
						text		: 'Save',
						formBind	: true,
						disabled	: false,
						autoWidth	: true,
						handler		: function()
						{
							var obj_Form = this.up('form').getForm();
							if (obj_Form.isValid())
							{
								obj_Form.submit(
								{
									url		: obj_config.C_STR_EXTERNAL_PATH+'/rest/savedQueries/data.php',
									waitMsg	: 'Saving Your Query',
									method	: "PUT",
									success	: function(form, action)
									{
										Ext.ComponentManager
										form.owner.ownerCt.close();
										m_obj_AmpleHarvest_ViewPort
												.fireEvent(AmpleHarvest.eventHandler.cWindowEvents.C_STR_EVENT_SAVED_QUERY_CHANGED);
										Ext.ComponentQuery.query('ampleHarvest-objects-querymanager-cquerymanager', m_obj_AmpleHarvest_ViewPort)[0].refresh();
//										Ext.MessageBox.alert("Success",
//												"Your query has been saved.",
//												close
//										);
									},
									failure	: function(form, action)
									{
										Ext.MessageBox.alert("Error",
												action.result.errors.msg, close
										);
									}
								}
								);
							}
						}
					}
			]
		}
		);

		this.add(this.m_obj_Form);

		this.m_obj_Form.add(Ext.create("Ext.form.field.Hidden",
		{
			name		: "id",
			disabled	: false,
			readOnly	: true,
			allowBlank	: true
		}
		));
		
		this.m_obj_sourcename = Ext.create("Ext.form.field.Hidden",
		{
			name		: "source_name",
			value		: this.source,
			readOnly	: true
		}
		);
		
		this.m_obj_Form.add(this.m_obj_sourcename);
		
		this.m_obj_Interface = Ext.create("Ext.form.field.Hidden",
		{
			name		: "source_id",
			readOnly	: true
		}
		);

		this.m_obj_Form.add(this.m_obj_Interface);

		this.m_obj_Query = Ext.create("Ext.form.field.Text",
		{
			fieldLabel	: "Query String",
			name		: "q_string",
			anchor		: "100%",
			disabled	: false,
			readOnly	: true,
			allowBlank	: false
		}
		);

		this.m_obj_Form.add(this.m_obj_Query);

		this.m_obj_Form.add(Ext.create("Ext.form.field.Text",
		{
			fieldLabel	: "Query Title",
			name		: "q_title",
			anchor		: "100%",
			inputType	: "textfield",
			disabled	: false,
			readOnly	: false,
			allowBlank	: false,
			blankText	: "Please enter a title for this query.",
			msgTarget	: 'side'
		}
		));

		this.m_obj_Schedule = Ext.create("Ext.form.field.ComboBox",
				{
					fieldLabel		: "Schedule",
					name			: "schedule",
					autoSelect		: true,
					typeAhead		: true,
					queryMode		: 'local',
					displayField	: 'Label',
					valueField		: 'Value',
					anchor			: "100%",
					store			: Ext
							.create("AmpleHarvest.DATA.Store.cDropDown")
				});
				
		this.m_obj_Schedule.store.set_ExtraParams(
		{
			id	: "C1645168-1F6B-4439-91D7-25460E481561"
		}
		);
		this.m_obj_Schedule.store.load();
		//this.m_obj_Schedule.doQuery("", true);

		this.m_obj_Form.add(this.m_obj_Schedule);
		//    	
		// this.m_obj_Form.add(Ext.create("AmpleHarvest.GUI.cKeyboardField"));

	},

	set_QueryString	: function(str_JSON)
	{
		this.m_obj_Query.setValue(str_JSON);
	},

	set_Interface	: function(str_Interface)
	{
		this.m_obj_Interface.setValue(str_Interface);
	},

	load_ID			: function(str_ID)
	{
		this.m_obj_Form.load(
		{
			url		: obj_config.C_STR_EXTERNAL_PATH+'/rest/savedQueries/data.php',
			params	:
			{
				id	: str_ID
			},
			method	: "get"
		}
		);
	},

	load_Rec		: function(obj_Rec)
	{
		alert("loading Rec");
		this.m_obj_Form.loadRecord(obj_Rec);
		this.set_Interface(obj_Rec.get("sourcename"))
	}
}
);